Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UMD support #3

Closed
wants to merge 4 commits into from
Closed

UMD support #3

wants to merge 4 commits into from

Conversation

zewa666
Copy link

@zewa666 zewa666 commented Apr 23, 2017

Adds UMD support for the libraries build artifacts

Related issue: #2

@zewa666
Copy link
Author

zewa666 commented Apr 23, 2017

As explained in the referenced issue I wouldn't see any easy way, for any module bundler to take full control, over when dependencies are properly timed. So an UMD approach seems to be the best to satisfy a broad variety of module formats.

Please do make sure this change doesn't break any pre-existing use cases, I didn't really find anything special but it would make sense to take some existing examples and properly check those.

I've also seen that you leverage gulptasks, so I felt free to modify those, but you might wanna think about creating a 3rd build articaft containing the umd exports

Copy link
Member

@simonbrunel simonbrunel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @zewa666 :) A few minor changes then I will make sure to test it and decide what's the best way to distribute the built files.

src/plugin.js Outdated
return 0;
} else if (typeof value === 'string' && value.indexOf('%') !== -1) {
return number / 100 * base;
var Chart = ChartJS || window.Chart;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the whole file indentation by removing the surrounding function make the PR almost unreadable and will inevitably conflict with the upcoming changes I'm working on based on Chart.js 2.6. Also, I would like to keep that file directly injectable (<script>) and thus preserve the global function scoping. That's why I prefer the following approach (inject the UMD param as function parameter):

/* global window: false, Chart */

'use strict';

(function(Chart) {

    // remove: var Chart = window.Chart;
    // and keep previous code unchanged
    // ...

})(Chart || window.Chart);

gulpfile.js Outdated
@@ -12,6 +12,7 @@ var zip = require('gulp-zip');
var merge = require('merge2');
var path = require('path');
var package = require('./package.json');
var umd = require('gulp-umd');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this require right after the var uglify = require('gulp-uglify'); one?

gulpfile.js Outdated
amd: 'chart.js',
cjs: 'chart.js',
global: 'Chart',
param: 'ChartJS'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the UMD param be renamed to Chart and reformat the array as:

return [{
  name: 'chart.js',
  amd: 'chart.js',
  cjs: 'chart.js',
  global: 'Chart',
  param: 'Chart'
}]

@zewa666
Copy link
Author

zewa666 commented Apr 23, 2017

Alright I've added the requested changes. One tip. How about rewriting the gulpfile to ES6 so it could be removed from the eslintignore list?

@simonbrunel
Copy link
Member

Looks good, thank, will try to test that very soon! The gulpfile is in the eslintignore because it doesn't use the same indentation rules as the plugin. Though, I already have some changes locally and can have a look to migrate to ES6.

That could be interesting to look at the other Chart.js hosted plugins to make them fully compatible with other environments such as Aurelia ;)

src/plugin.js Outdated
@@ -1,4 +1,4 @@
/* global window: false, ChartJS */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, Chart needs to be defined in the global (see Travis build)

@simonbrunel simonbrunel added this to the 1.0.0 milestone Apr 23, 2017
@zewa666
Copy link
Author

zewa666 commented Apr 24, 2017

@simonbrunel wonder why my local eslint didn't capture that ... strange. Good catch thanks

@simonbrunel
Copy link
Member

simonbrunel commented Apr 24, 2017

gulp-umd uses the filename (capitalized) as the global namespace and so tries to set window.Plugin = Plugin, which doesn't look nice. Also, the plugin doesn't export anything and module.exports = undefined doesn't look good either.

It's possible to configure the target namespace but since gulp-umd doesn't do a lot and seems not anymore maintained, I think it's easier to directly implement UMD inside plugin.js:

'use strict';

;(function(root, factory) {
	if (typeof define === 'function' && define.amd) {
		define(['chart.js'], factory);
	} else if (typeof exports === 'object') {
		module.exports = factory(require('chart.js'));
	} else {
		factory(root.Chart);
	}
}(this, function(Chart) {
	var helpers = Chart.helpers;

        // ...

	var plugin = {
		beforeInit: function(instance) {
		// ...
	};

	Chart.plugins.register(plugin);
	return plugin;
}));

@zewa666 what do you think?

@zewa666
Copy link
Author

zewa666 commented Apr 24, 2017

Fewer dependencies is always better. I was just unsure whether you'd like to have it as part of your gulp build, but if you're fine with placing it in plugin.js it's definitely fine as well

@simonbrunel
Copy link
Member

@zewa666 I'm fine with having it in plugin.js, do you want to update that PR or should I implement it in my upcoming changes?

@zewa666
Copy link
Author

zewa666 commented Apr 27, 2017

hey @simonbrunel, yeah if you don't mind that would be easier. I'll close this PR thanks for the reviews. 👍

@zewa666 zewa666 closed this Apr 27, 2017
@simonbrunel
Copy link
Member

simonbrunel commented Apr 27, 2017

Thanks for your help and time :)

@simonbrunel simonbrunel removed this from the 1.0.0 milestone Apr 27, 2017
@simonbrunel
Copy link
Member

After investigating a bit more, I think I will use rollup which generates a better UMD code than gulp-umd.

@zewa666
Copy link
Author

zewa666 commented Apr 27, 2017

Sure, which also has a lot of traction these days. Good choice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants